package com.example.yangguoying;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.yangguoying.SqLiteDatabase.GetDatabase;
import com.example.yangguoying.entity.StudentInfo;
import com.example.yangguoying.util.SolveEditTextScrollClash;

public class AddStudentInfoActivity extends AppCompatActivity {

    EditText add_student_name;
    EditText add_student_num;
    EditText add_student_phone;
    EditText add_student_faculty;
    EditText add_student_grade;
    EditText add_student_class;
    EditText add_student_dormitory;
    EditText add_student_address;
    EditText add_student_remarks;
    Button add_student_info;
    ContentValues contentValues;
    GetDatabase database;
    SQLiteDatabase sqLiteDatabase;
    ImageButton bt_black;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_student_info);
        initView();
        setView();
    }

    @Override
    public void onBackPressed() {
        showAlertDialog();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        add_student_name = findViewById(R.id.add_student_name);
        add_student_num = findViewById(R.id.add_student_num);
        add_student_phone = findViewById(R.id.add_student_phone);
        add_student_faculty = findViewById(R.id.add_student_faculty);
        add_student_grade = findViewById(R.id.add_student_grade);
        add_student_class = findViewById(R.id.add_student_class);
        add_student_dormitory = findViewById(R.id.add_student_dormitory);
        add_student_address = findViewById(R.id.add_student_address);
        add_student_remarks = findViewById(R.id.add_student_remarks);
        add_student_info = findViewById(R.id.add_student_info);
        add_student_remarks.setOnTouchListener(new SolveEditTextScrollClash(add_student_remarks));
        contentValues = new ContentValues();
        database = new GetDatabase();
        sqLiteDatabase = database.getDatabase(this).getWritableDatabase();
        bt_black = findViewById(R.id.bt_back);
    }

    private void setView() {
        add_student_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 添加数据
                insertStudentInfo();
            }
        });

        bt_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });

    }

    private void showAlertDialog(){
        String studentName = getEditTextString(add_student_name);
        String studentNum = getEditTextString(add_student_num);
        String studentPhone = getEditTextString(add_student_phone);
        String studentAddress = getEditTextString(add_student_address);
        String studentFaculty = getEditTextString(add_student_faculty);
        String studentGrade = getEditTextString(add_student_grade);
        String studentClass = getEditTextString(add_student_class);
        String studentDormitory = getEditTextString(add_student_dormitory);
        String studentRemarks = getEditTextString(add_student_remarks);
        if (!studentName.equals("") || !studentNum.equals("") || !studentPhone.equals("") || !studentAddress.equals("") || !studentFaculty.equals("") || !studentGrade.equals("") || !studentClass.equals("") || !studentDormitory.equals("") || !studentRemarks.equals("")){
            AlertDialog.Builder builder = new AlertDialog.Builder(AddStudentInfoActivity.this);
//           设置提示框标题
            builder.setTitle("提示");
//            设置提示框提示文字
            builder.setMessage("离开将会清除所有填写数据，是否保存！");
//            监听提示框点击保存按钮
            builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
//                    保存数据到数据库方法
                    insertStudentInfo();
                }
            });
//            监听用户点击提示框取消按钮
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
//            让提示框显示出来
            builder.show();
        }else{
//            关闭页面
            startActivity(new Intent(AddStudentInfoActivity.this,MainActivity.class));
            finish();
        }
    }


    private void insertStudentInfo(){
        String studentName = getEditTextString(add_student_name);
        String studentNum = getEditTextString(add_student_num);
        String studentPhone = getEditTextString(add_student_phone);
        String studentAddress = getEditTextString(add_student_address);
        String studentFaculty = getEditTextString(add_student_faculty);
        String studentGrade = getEditTextString(add_student_grade);
        String studentClass = getEditTextString(add_student_class);
        String studentDormitory = getEditTextString(add_student_dormitory);
        String studentRemarks = getEditTextString(add_student_remarks);
        int isUpload = 0;

        if (!studentName.equals("") && !studentNum.equals("") && !studentPhone.equals("") && !studentAddress.equals("") && !studentFaculty.equals("") && !studentGrade.equals("") && !studentClass.equals("") && !studentDormitory.equals("") && !studentRemarks.equals("")){
            if (studentNum.length() > 12){
                Toast.makeText(AddStudentInfoActivity.this,"学号不能高于12位",Toast.LENGTH_SHORT).show();
                return;
            }
            if (studentPhone.length() > 11){
                Toast.makeText(AddStudentInfoActivity.this,"请填写正确的电话号码",Toast.LENGTH_SHORT).show();
                return;
            }
            contentValues.put("student_name",studentName);
            contentValues.put("student_num",studentNum);
            contentValues.put("student_phone",studentPhone);
            contentValues.put("student_address",studentAddress);
            contentValues.put("student_faculty",studentFaculty);
            contentValues.put("student_grade",studentGrade);
            contentValues.put("student_class",studentClass);
            contentValues.put("student_dormitory",studentDormitory);
            contentValues.put("student_remarks",studentRemarks);
            contentValues.put("is_upload", isUpload);
            if (queryStudentInfo(studentNum)){
                Toast.makeText(AddStudentInfoActivity.this,"学生学号已存在",Toast.LENGTH_SHORT).show();
            }else {
                sqLiteDatabase.insert("student_info",null,contentValues);
                Toast.makeText(AddStudentInfoActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                contentValues.clear();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(new Intent(AddStudentInfoActivity.this,MainActivity.class));
                finish();
            }

        }else {
            Toast.makeText(AddStudentInfoActivity.this,"请填写必填参数！", Toast.LENGTH_SHORT).show();
        }

    }

    private String getEditTextString(EditText editText){
        return editText.getText().toString();
    }

    private boolean queryStudentInfo(String student_num){
        cursor = sqLiteDatabase.query("student_info",null,"student_num = ?",new String[]{student_num},null,null,null,null);
        return cursor.moveToFirst();
    }
}